package com.servlet;

import java.io.IOException;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.model.Orderitem;
import com.model.Orders;
import com.model.Shopcart;
import com.model.User;
import com.service.OrderItemService;
import com.service.OrderService;
import com.service.ShopcartService;
import com.util.Conversion;
import com.util.JdbcUtil;
import com.util.PageUtil;

/**
 * Servlet implementation class OrderServlet
 */
@WebServlet("/order")
public class OrderServlet extends BaseServlet  {
	JdbcUtil jdbc=new JdbcUtil();
	OrderService orderService=new OrderService();
	OrderItemService orderItemService = new OrderItemService();
	ShopcartService cartService = new ShopcartService();
	//检查是否登录
	public boolean checkUserIsLogin(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
		User user = (User)request.getSession().getAttribute("user");
		if(user==null){
			String mess="暂未登录，请登录后再操作！";
			request.setAttribute("mess",mess);
			request.getRequestDispatcher("userLogin.jsp").forward(request, response);
			return false;
		}	
		return true;
	}
	//随机生成订单号
	public String getOrderIdByTime() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String newDate=sdf.format(new Date(0));
		String result="";
		Random	random=new Random();
		for(int i=0;i<3;i++){
			result+=random.nextInt(10);
		}
	return(newDate+result);
	}
	//用户添加订单
	private void add_user(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if(!checkUserIsLogin(request,response)){
			return;
		}
		Orders orders = new Orders();
		//获取订单号
		String oid=getOrderIdByTime();
		Conversion.convert(orders, request);
		//设置下单时间
		orders.setSubmittime(new Timestamp(System.currentTimeMillis()));
		orders.setOstatus("已下单");
		orders.setOid(oid);
		System.out.println(orders.getDestinetime());
		try {
			orderService.add(orders);
			
			String[] pids=request.getParameterValues("pid");
			String[] pnames=request.getParameterValues("pname");
			String[] pimages=request.getParameterValues("pimage");
			String[] pprices=request.getParameterValues("pprice");
			String[] weights=request.getParameterValues("weight");
			String[] tprices=request.getParameterValues("tprice");
			String uid=request.getParameter("uid");
			String eid =null;
			eid =request.getParameter("eid");
			for(int i =0;i<tprices.length;i++){
				//设置子订单号
				String ooid ="详情"+(i+1);
				System.out.println(ooid+" "+weights[i]);
				Orderitem item = new Orderitem(ooid,oid,Integer.parseInt(uid),
				Integer.parseInt(pids[i]),null,pnames[i],pimages[i],
				Double.parseDouble(pprices[i]),Double.parseDouble(weights[i]),
				Double.parseDouble(tprices[i]));
				orderItemService.add(item);
				System.out.println("Item执行完成");
			}
			cartService.deleteByUid(Integer.parseInt(uid));
			response.sendRedirect("order?action=query_user");
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	
	}
	private void query_user(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if(!checkUserIsLogin(request,response)){
			return;
		}
		Integer cp = Integer.parseInt(request.getParameter("cp")==null?"1":request.getParameter("cp"));
		Integer pagesize=8;
		List<Orders> orders = null;
		User user = (User)request.getSession().getAttribute("user");
		Orders order = new Orders();
		order.setUid(user.getUid());
		
		orders = orderService.queryOrders_show(order, cp, pagesize);
		int count = orderService.count_user(order);
		
		String pageTool = PageUtil.getPageTool(request, count, cp, pagesize);
		request.setAttribute("pageTool",pageTool);
		request.setAttribute("orders",orders);
		System.out.println(orders);
		request.getRequestDispatcher("order.jsp").forward(request, response);
	}
	//用户取消刚下单的订单
	private void cancelOrder_ajax(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String oid = request.getParameter("oid");
		String ostatus="已取消";
		System.out.println(oid);
		try {
			orderService.updateOrderStatus(oid, ostatus);
			System.out.println(oid+ostatus);
			response.getWriter().write("1");
		} catch (Exception e) {
			e.printStackTrace();
			response.getWriter().write("0");
		}
	}
}
